import {
  REGISTER_SUCCESS, REGISTER_FAIL,
  LOGIN_SUCCESS, LOGIN_FAIL,
  ADD_NEW_TASK_SUCCESS, ADD_NEW_TASK_FAIL,
  GET_DAY_TASK_SUCCESS, GET_DAY_TASK_FAIL,
  GET_TASK_DATE_LIST_SUCCESS, GET_TASK_DATE_LIST_FAIL,
  GET_ALL_TASK_SUCCESS, GET_ALL_TASK_FAIL,
  CHANGE_TASK_STATE_SUCCESS, CHANGE_TASK_STATE_FAIL,
} from './actionTypes';
import {request} from '../../api/request';
import {message} from 'antd';

const CODE_SUCCESS = '00000000';

// 注册成功同步action
const register_success = msg => ({type: REGISTER_SUCCESS, data: msg});

// 注册失败同步action
const register_fail = msg => ({type: REGISTER_FAIL, data: msg});

// 登陆成功同步action
const login_success = msg => ({type: LOGIN_SUCCESS, data: msg});

// 登陆失败同步action
const login_fail = msg => ({type: LOGIN_FAIL, data: msg});

// 新增任务成功同步action
const add_new_task_success = bool => ({type: ADD_NEW_TASK_SUCCESS, data: bool});

// 新增任务失败同步action
const add_new_task_fail = bool => ({type: ADD_NEW_TASK_FAIL, data: bool});

// 获取某天任务成功同步action
const get_day_task_success = (task, time) => ({type: GET_DAY_TASK_SUCCESS, data: task, time});

// 获取某天任务失败同步action
const get_day_task_fail = msg => ({type: GET_DAY_TASK_FAIL, data: msg});

// 获取任务日期列表成功同步action
const get_task_date_list_success = dateList => ({type: GET_TASK_DATE_LIST_SUCCESS, data: dateList});

// 获取任务日期列表失败同步action
const get_task_date_list_fail = msg => ({type: GET_TASK_DATE_LIST_FAIL, data: msg});

// 获取单个用户所有任务成功同步action
const get_all_task_success = task => ({type: GET_ALL_TASK_SUCCESS, data: task});

// 获取单个用户所有任务失败同步action
const get_all_task_fail = msg => ({type: GET_ALL_TASK_FAIL, data: msg});

// 改变任务状态成功同步action
const change_task_state_success = bool => ({type: CHANGE_TASK_STATE_SUCCESS, data: bool});

// 改变任务状态成功失败action
const change_task_state_fail = msg => ({type: CHANGE_TASK_STATE_FAIL, data: msg});

// 注册异步action
export const register = values => {
  return async dispatch => {
    const res = await request('/api/register', {values}, 'post');
    if(res.data.TagCode === CODE_SUCCESS) {
      dispatch(register_success(res.data.TagMsg));
      message.success(res.data.TagMsg);
    } else {
      dispatch(register_fail(res.data.TagMsg));
      message.error(res.data.TagMsg);
    }
  }
}

// 登陆异步action
export const login = values => {
  return async dispatch => {
    const res = await request('/api/login', {values}, 'post');
    if(res.data.TagCode === CODE_SUCCESS) {
      dispatch(login_success(res.data.TagMsg));
      message.success(res.data.TagMsg);
      window.location.reload();
    } else {
      dispatch(login_fail(res.data.TagMsg));
      message.error(res.data.TagMsg);
    }
  }
}

// 新增任务异步action
export const add_new_task = value => {
  return async dispatch => {
    const res = await request('/api/add_new_task', {value}, 'post');
    if(res.data.TagCode === CODE_SUCCESS) {
      dispatch(add_new_task_success(res.data.data));
    } else {
      dispatch(add_new_task_fail(res.data.data));
      message.warn(res.data.TagMsg);
    }
  }
}

// 获取某天任务异步action
export const get_day_task = day => {
  return async dispatch => {
    // 这里的day参数是时间戳
    const res = await request('/api/tasklist_day', {day});
    if(res.data.TagCode === CODE_SUCCESS) {
      dispatch(get_day_task_success(res.data.data, res.data.time));
    } else {
      dispatch(get_day_task_fail(res.data.TagMsg));
      message.error(res.data.TagMsg);
    }
  }
}

// 获取任务日期列表异步action
export const get_task_date_list = () => {
  return async dispatch => {
    const res = await request('/api/tasklist_date', {});
    if(res.data.TagCode === CODE_SUCCESS) {
      dispatch(get_task_date_list_success(res.data.data));
    } else {
      dispatch(get_task_date_list_fail(res.data.TagMsg));
    }
  }
}

// 获取单个用户所有任务异步action
export const get_all_task = () => {
  return async dispatch => {
    const res = await request('/api/single_user_all_task', {});
    if(res.data.TagCode === CODE_SUCCESS) {
      dispatch(get_all_task_success(res.data.data));
    } else {

    }dispatch(get_all_task_fail(res.data.TagMsg));
  }
}

// 改变任务状态异步action
export const change_task_state = (day, task_state1, task_state2, task_index) => {
  return async dispatch => {
    const res = await request('/api/change_task_state', {day, task_state1, task_state2, task_index}, 'post');
    if(res.data.TagCode === CODE_SUCCESS) {
      dispatch(change_task_state_success(res.data.data));
      message.success('move item success !');
      const result = await request('/api/tasklist_day', {day});
      if(result.data.TagCode === CODE_SUCCESS) {
        dispatch(get_day_task_success(result.data.data, result.data.time));
      } else {
        dispatch(get_day_task_fail(result.data.TagMsg));
        message.error(result.data.TagMsg);
      }
    } else {
      dispatch(change_task_state_fail(res.data.TagMsg));
      message.error(res.data.TagMsg);
    }
  }
}
